var vm = new Vue({
    el:"#app",
    data(){
        return {
            TelephoneNo:'',
            Store:'',
            modal1: false, //兑换礼品录入弹框
            FormItem:{
                Store:'',
                Bm:'',
                UserNo:'',
                UserName:'',
                Tel:'',
                Gift:''
            },
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '手机号',
                    key: 'Tel',
                    align: 'center',
                },
                {
                    title: '兑换礼品',
                    key: 'Gift',
                    align: 'center',
                    width:300
                },
                {
                    title: '店面',
                    key: 'Store',
                    align: 'center',
                },
                {
                    title: '兑换日期',
                    key: 'GetDate',
                    align: 'center',

                },
                {
                    title: '经办人',
                    key: 'InputMan',
                    align: 'center',
                },
                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:150,
                    render: (h, params) => {
                        return h('div', [
                            /*h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        this.edit();
                                    }
                                }
                            },'编辑'),
                            h('span', {
                            },'|'),*/
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        this.delGiftInfo(params);
                                    }
                                }
                            },'删除')
                        ])
                    }
                }
            ],
            data1: [],

        }
    },
    created(){
        this.initThisPage();
    },
    methods:{
        ok () {
        },
        cancel () {
        },
        // 编辑弹框
        edit(){
            this.modal1=true;
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goLogin/initIndexPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }else{
                        self.FormItem.Store = data.Store;
                        self.FormItem.Bm = data.Bm;
                        self.FormItem.UserNo = data.UserNo;
                        self.FormItem.UserName = data.UserName;
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        saveGift(){
            var self = this;
            var Store = self.FormItem.Store;
            var Bm = self.FormItem.Bm;
            var UserNo = self.FormItem.UserNo;
            var UserName = self.FormItem.UserName;
            var Tel = self.FormItem.Tel;
            var Gift = self.FormItem.Gift;
            if(Tel==null||Tel==""){
                toastr.warning("请输入手机号");
                return false;
            }
            if(!self.isPoneAvailable(Tel)){
                toastr.warning("请输入正确格式的手机号");
                return false;
            }
            if(Gift==null||Gift==""){
                toastr.warning("请录入礼品");
                return false;
            }
            var jsondata = {
                "Store":Store,
                "Bm":Bm,
                "UserNo":UserNo,
                "UserName":UserName,
                "Tel":Tel,
                "Gift":Gift,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/saveGift", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==1){
                        toastr.success("保存成功");
                        self.FormItem.Tel = '';
                        self.FormItem.Gift = '';
                        self.querytable();
                    }else if(data.Status==-1){
                        toastr.warning("手机号已存在，无法兑换商品");
                    }else{
                        toastr.error("保存失败，请刷新页面后重试");
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        delGiftInfo(params){
            var self = this;
            var ID = params.row.ID;
            var UserNo = params.row.InputManNo;
            var jsondata = {
                "ID":ID,
                "UserNo":UserNo
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/delGiftInfo", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==1){
                        toastr.success("删除成功");
                        self.querytable();
                    }else if(data.Status==-1){
                        toastr.warning("删除失败,不可删除其他录入人信息");
                    }else{
                        toastr.error("删除失败，请刷新页面后重试");
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        querytable(){
            var self = this;
            var info ;
            var TelephoneNo = self.TelephoneNo;
            var Store = self.Store;
            var jsondata={"Tel":TelephoneNo,"Store":Store};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProject/queryAllGifts", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                },
                error: function() {},
            });
            if(info){
                self.data1 = info;
            }
        },

        //手机号验证
        isPoneAvailable(str) {
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
    }
})







